package mephisto.services.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Utils {

    public Utils() {
        super();
    }
    
    public String encode(String s) throws UnsupportedEncodingException {
        // return new String(Base64.encode(s.getBytes(), false));
        return URLEncoder.encode(s, "UTF-8");

    }

    public String decode(String s) throws UnsupportedEncodingException {
        // return new String(Base64.decode(s));
        return URLDecoder.decode(s, "UTF-8");

    }

    public static void main(String[] args) {

        Utils mephisto = new Utils();
        String sparql = "select distinct ?person ?label where {{?person a nao:Party} . OPTIONAL {?person nao:prefLabel ?label}} order by ?label ?person offset 0 limit 10";
        try {
            //Object output = mephisto.runQuery(sparql);
            //System.out.println(output);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Request request = new Request(Method.GET,
        // "https://api.del.icio.us/v1/posts/recent");
        // request.setChallengeResponse(new
        // ChallengeResponse(ChallengeScheme.HTTP_BASIC, "arkub", "56006"));
        //
        // // Fetch a resource: an XML document with your recent posts
        // Response response = new Client(Protocol.HTTPS).handle(request);

    }

    public static String streamToString(InputStream is) throws IOException {
        byte[] byteData= streamToBytes(is);
        String result = new String(byteData, "UTF-8");
        return result;

    }
    
    public static byte[] streamToBytes(InputStream is) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] bytes = new byte[512];
     
        // Read bytes from the input stream in bytes.length-sized chunks and write
        // them into the output stream
        int readBytes;
        while ((readBytes = is.read(bytes)) > 0) {
            os.write(bytes, 0, readBytes);
        }
     
        // Convert the contents of the output stream into a byte array
        byte[] byteData = os.toByteArray();
        
        // Close the streams
        is.close();
        os.close();
        return byteData;
    }
 

}
